/**
 * 图文混排 2017-02-22 Juliana
 */
(function (W, $) {
    "use strict";
    W.imgText = {
        /**图片处理**/
        //初始化图片
        initImgNew: function (tagContent) {
            var that = this;
            var imgs = $(tagContent).find('img');
            var defW = $(tagContent).width();
            for (var i = 0; i < imgs.length; i++) {
                var img = imgs[i];
                var src = $(img).attr('src');
                $(img).attr('style', '');
                if (src.indexOf('http://') != -1 || src.indexOf('https://') != -1) {
                    $(img).attr('data-original', src + '?imageMogr2/auto-orient/thumbnail/750x');
                    // $(img).attr('src', src+'?imageMogr2/auto-orient/thumbnail/!80p');
                    $(img).attr('srci', src);
                } else {
                    if (src.indexOf('data:image/jpeg;base64') == -1) {
                        $(img).attr('data-original', window.serverUrl + src);
                        $(img).attr('srci', window.serverUrl + src);
                    } else {
                        $(img).attr('data-original', src + '?imageMogr2/auto-orient/thumbnail/750x');
                        // $(img).attr('src', src+'?imageMogr2/auto-orient/thumbnail/!80p');
                        $(img).attr('srci', src);
                    }
                }
                if ($(img).data('width') != 'undefined' && typeof $(img).data('width') != 'undefined' && $(img).data('width') != null && $(img).data('width') != '') {
                    $(img).attr('src', '');
                    var realW = $(img).data('width'), ratio = $(img).data('ratio'), realH = realW / ratio;
                    var retW, retH;
                    if (realW > defW) {
                        retW = defW, retH = defW / ratio;
                    } else {
                        retW = realW, retH = realH;
                        $(img).attr('data-original', src);
                    }
                    $(img).attr('width', retW);
                    $(img).attr('height', retH);
                } else {
                    if(src.indexOf('image.myqcloud.com') != -1){
                        $(img).attr('src', src + '?imageMogr2/auto-orient/thumbnail/750x');
                        $(img).attr('data-original', src + '?imageMogr2/auto-orient/thumbnail/750x');
                    }else{
                        $(img).attr('data-original', src);
                    }
                    // $(img).attr('src', window.serverUrl + '/micWeb/image/bg_gray.png');
                    $(img).css('max-width', '100%');
                    $(img).attr('width', '');
                    $(img).attr('height', '');
                }

                $(img).addClass('lazy-img');
                $(img).attr('onerror', 'imgText.errorImg(this)');
                $(img).attr('name', 'imgTextPic');
                var parent = $(img).parent();
                parent.css('text-align', 'center');
                parent.css('display', 'block');
                // img.onclick = function () {
                //     common.openImage(this);
                // }
            }
            that.initIframe(tagContent);
            $(tagContent).fadeIn();
        },
        initIframe: function (tagContent) {
            var minW = 400
            var $iframs = $(tagContent).find('iframe');
            var defW = $(tagContent).width();
            var frameW = defW < minW ? minW : defW >= minW-100 ? defW-100 : minW
            var frameH = frameW * 3/4
            $iframs.each(function (i, iframe) {
                $(iframe).attr('src', $(iframe).attr('src').replace('http://', '//').replace('https://', '//'))
                $(iframe).attr('width', frameW)
                $(iframe).attr('height', frameH)
                $(iframe)[0].outerHTML = '<div style="position:relative;z-index:1;height:' + frameH + 'px;">' +
                    $(iframe)[0].outerHTML + '</div>'
            })
        },
        changeWH: function (img) {
            var width = $(img).closest('.img-text-content')[0].clientWidth;
            img.style.maxWidth = width * 0.9 + "px";
            img.style.maxHeight = (width * 0.9 / img.naturalWidth * img.naturalHeight) + "px";
        },
        errorImg: function (img) {
            img.src = window.serverUrl + '/image/404.png';
        }
    }

    $.fn.picLazyLoad = function (settings) {
        var $this = $(this), _winScrollTop = 0, _winHeight = $(window).height();
        settings = $.extend({
            threshold: 100,
            placeholder: window.serverUrl+'/image/img_bg.png',
            parent: $('.img-text-content')
        }, settings);
        lazyLoadPic();
        $('.index-outside>section').on('scroll', function () {
            _winScrollTop = $(window).scrollTop();
            lazyLoadPic();
        });
        $(window).on('scroll', function () {
            _winScrollTop = $(window).scrollTop();
            lazyLoadPic();
        });
        function lazyLoadPic() {
            $this.each(function () {
                var $self = $(this);
                if ($self.is('img')) {
                    if ($self.attr('data-original')) {
                        var _offsetTop = $self.offset().top;
                        var rect = settings.parent[0].getBoundingClientRect();
                        if (_offsetTop <= rect.height + settings.parent.offset().top) {
                            if ((_offsetTop - settings.threshold) <= (_winHeight + _winScrollTop)) {
                                var realUrl = $self.attr('data-original');
                                $self.attr('src', realUrl);
                                $self.removeAttr('data-original');
                            }
                        }
                    }
                } else if ($self.attr('data-original')) {
                    if ($self.css('background-image') == 'none') {
                        $self.css('background-image', 'url(' + settings.placeholder + ')');
                    }
                    var _offsetTop = $self.offset().top;
                    var rect = settings.parent[0].getBoundingClientRect();
                    if (_offsetTop <= rect.height + settings.parent.offset().top) {
                        if ((_offsetTop - settings.threshold) <= (_winHeight + _winScrollTop)) {
                            var realUrl = $self.attr('data-original');
                            $self.css('background-image', 'url(' + realUrl + ')');
                            $self.removeAttr('data-original');
                        }
                    }
                }
            });
        }
    }
}(window, $));